// pages/operation/signUp/signUpSale/index.js
import {
  SignUpModel
} from "../../../../api/api-work/signUpModel.js"
const signUpModel = new SignUpModel()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    num: 10,
    saleIndex: "", //操作下标
    salerDatas: [], //销售人员下拉列表
    salerList: [{
      saler: "", //业绩归属人id
      salerName: "",
      courseAmount: "", //销售业绩
    }],

    isShowSaler: false, //业绩归属人弹窗开关
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const params = JSON.parse(options.param);
    console.log('params', params);
    this.setData({
      salerList: params
    })

  },

  /**
   * 生命周期函数--监听页面显示
   */
  async onShow() {
    await this.getSaleDatas()
  },
  //获取销售人员
  async getSaleDatas() {
    signUpModel.getPerformanceListAPI({
      pageNum: 1,
      pageSize: 9999
    }).then(res => {
      console.log('7777', res);
      this.setData({
        salerDatas: res.rows
      })
    })
  },
  //打开销售人员弹窗
  showSalePopup(e) {
    console.log('index', e.currentTarget.dataset.query);

    this.setData({
      isShowSaler: true,
      saleIndex: e.currentTarget.dataset.query
    })
  },
  //关闭销售人员弹窗
  onCloseSalePopup() {
    this.setData({
      isShowSaler: false
    })
  },
  // 选择销售人员
  selectSaler(e) {
    const query = e.currentTarget.dataset.query;
    this.setData({
      isShowSaler: false,
      [`salerList[${this.data.saleIndex}].salerName`]: query.nickName,
      [`salerList[${this.data.saleIndex}].saler`]: query.id,
    });
  },
  //填写销售业绩
  onChangeCourseAmount(e) {
    const index = e.currentTarget.dataset.query;
    this.setData({
      [`salerList[${index}].courseAmount`]: e.detail
    });
  },
  //增加业绩归属人  
  addItem() {
    const salerList = this.data.salerList
    salerList.push({
      saler: "", // 业绩归属人id
      salerName: "",
      courseAmount: "", // 销售业绩
    });
    this.setData({
      salerList: [...salerList]
    });
  },
  //去除业绩归属人
  deleteItem(e) {
    const index = e.currentTarget.dataset.query;
    this.data.salerList.splice(index, 1)
    this.setData({
      salerList: this.data.salerList
    });
  },
  //确认按钮
  confirm() {
    // wx.setStorage({
    //   key: 'someKey',
    //   data: {
    //     saleData: this.data.salerList
    //   },
    // });
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2]; // 上一个页面
    if (this.data.salerList.length) {
      prevPage.setData({
        saleList: this.data.salerList
      });
    } else {
      prevPage.setData({
        saleList: [{
          saler: "", //业绩归属人id
          salerName: "",
          courseAmount: "", //销售业绩
        }],
      });
    }

    wx.navigateBack()
  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})